﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;

public partial class Controls_Template_ThongTinKyThuatChiTiet : System.Web.UI.UserControl
{
    int SIZE_PART = 5;
    class_database db;

    protected void drpPermission_SelectedIndexChanged(object sender, EventArgs e) 
    {
        DataTable dt = new DataTable();
        db = new class_database();
        string value = drpPermission.SelectedValue.ToString();
        string idMau = Session["idMau"].ToString();
        string[] _param = { "permission" };
        string[] _value = { value };
        string _where = "idmau = " + idMau + " AND idloaibai = 2";
        db.UpdateRecord("tbl_baiphantich", _param, _value, _where);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string idMau = Convert.ToString(Session["idMau"]);
        for (int i = 1; i <= SIZE_PART; i++)
        {
            LoadData(i, idMau);
        }
        if (!IsPostBack)
        {
            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "ckfinder";
            _FileBrowser.SetupCKEditor(CKEditor1);
        }
        SetDisplayEditButton();
        SetTitle(idMau);
    }

    protected void SetDisplayEditButton()
    {
        string sessionUserType = Session["user_type"].ToString();
        string sessionUser = Session["username"].ToString();
        string idMau = Session["idMau"].ToString();
        string nguoiPhanTich = GetNguoiPhanTich(idMau);

        if (sessionUserType == "0")
        {
            drpPermission.Attributes.Add("style", "display: inline-block");
            string sqlGetPermission = "SELECT permission FROM tbl_baiphantich WHERE idloaibai = 2 AND idmau = " + idMau;
            db = new class_database();
            DataTable dt = db.GetDataTable(sqlGetPermission);
            if (dt != null && dt.Rows.Count > 0)
            {
                drpPermission.SelectedValue = dt.Rows[0]["permission"].ToString();
            }
        }

        if (sessionUserType == "0" || nguoiPhanTich.ToLower() == sessionUser)
        {
            btnEditTTKTCT.Attributes.Add("style", "display: inline-block");
            btnDeleteThongTinKyThuatChiTiet.Attributes.Add("style", "display: inline-block");
        }
    }

    protected string GetNguoiPhanTich(string idMau)
    {
        string sql = "SELECT tbl_user.name FROM tbl_mau, tbl_user WHERE tbl_mau.nguoiphantich = tbl_user.id AND tbl_mau.id = " + idMau;
        db = new class_database();
        DataTable dt = db.GetDataTable(sql);
        string nameNguoiPhanTich = "";
        if (dt != null && dt.Rows.Count > 0)
        {
            nameNguoiPhanTich = dt.Rows[0]["name"].ToString();
        }
        return nameNguoiPhanTich;
    }

    protected void SetTitle(string idMau)
    {
        string sql = "SELECT tbl_mau.tenhamdiet, tbl_mau.date, tbl_user.name FROM tbl_mau, tbl_user WHERE tbl_mau.nguoiphantich = tbl_user.id AND tbl_mau.id = " + idMau;
        db = new class_database();
        DataTable dt = db.GetDataTable(sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            lblNguoiPhanTich.Text = "Người phân tích: " + dt.Rows[0]["name"].ToString();
            lblNgay.Text = "Ngày: " + dt.Rows[0]["date"].ToString();
            lblTenHamDiet.Text = "Tên hàm diệt: " + dt.Rows[0]["tenhamdiet"].ToString();
        }
    }

    protected void btnDeleteThongTinKyThuatChiTiet_Click(object sender, EventArgs e)
    {
        if (hdnCheckConfirm.Value == "yes")
        {
            string idMau = Session["idMau"].ToString();
            db = new class_database();
            string _where = "idmau = " + idMau;
            db.DeleteRecord("tbl_ttkt_detail", _where);
            string _where2 = "idloaibai = 2 AND idmau = " + idMau;
            db.DeleteRecord("tbl_baiphantich", _where2);
            Response.Redirect("Default.aspx?module=Template&view=General&idMau=" + idMau);
        }
    }

    protected void btnEditTTKTCT_Click(object sender, EventArgs e)
    {
        btnEditHanhViKyThuat.Attributes.Add("style", "display: block");
        btnEditKeyDacBiet.Attributes.Add("style", "display: block");
        btnEditKetNoiMang.Attributes.Add("style", "display: block");
        btnEditMoTaChiTiet.Attributes.Add("style", "display: block");
        btnEditHanhViMalware.Attributes.Add("style", "display: block");
    }



    protected void btnEditHanhViKyThuat_Click(object sender, EventArgs e) 
    {
        hdnPart.Value = "1";
        hdnIDMau.Value = Session["idMau"].ToString();
        ckMota.Text = ltrHanhViKyThuat.Text;
        btnEditHanhViKyThuat.Visible = false;
        divCKMota.Attributes.Add("style", "display: block");        
    }

    protected void btnOKMoTa_Click(object sender, EventArgs e)
    {
        string part = hdnPart.Value;
        string idMau = hdnIDMau.Value;
        string[] _param = { "noidung", "noidungdecode" };
        string noiDungDecode = Regex.Replace(Server.HtmlDecode(ckMota.Text), "<.*?>", string.Empty);
        string[] _value = { ckMota.Text, noiDungDecode };
        string _where = "typeid = " + part + " AND idmau = " + idMau;
        db = new class_database();
        db.UpdateRecord("tbl_ttkt_detail", _param, _value, _where);
        divCKMota.Attributes.Add("style", "display: none");
        Response.Redirect(Request.RawUrl, true);
    }

    protected void btnCancelMoTa_Click(object sender, EventArgs e)
    {
        divCKMota.Attributes.Add("style", "display: none");
        btnEditHanhViKyThuat.Visible = true;
    }

    protected void btnEditKeyDacBiet_Click(object sender, EventArgs e) 
    {
        hdnPart.Value = "2";
        hdnIDMau.Value = Session["idMau"].ToString();
        ckKeyDacBiet.Text = ltrKeyDacBiet.Text;
        btnEditKeyDacBiet.Visible = false;
        divCKKeyDacBiet.Attributes.Add("style", "display: block");
    }

    protected void btnOKKeyDacBiet_Click(object sender, EventArgs e)
    {
        string part = hdnPart.Value;
        string idMau = hdnIDMau.Value;
        string[] _param = { "noidung", "noidungdecode" };
        string noiDungDecode = Regex.Replace(Server.HtmlDecode(ckKeyDacBiet.Text), "<.*?>", string.Empty);
        string[] _value = { ckKeyDacBiet.Text, noiDungDecode };
        string _where = "typeid = " + part + " AND idmau = " + idMau;
        db = new class_database();
        db.UpdateRecord("tbl_ttkt_detail", _param, _value, _where);
        divCKKeyDacBiet.Attributes.Add("style", "display: none");
        Response.Redirect(Request.RawUrl, true);
    }

    protected void btnCancelKeyDacBiet_Click(object sender, EventArgs e)
    {
        divCKKeyDacBiet.Attributes.Add("style", "display: none");
        btnEditKeyDacBiet.Visible = true;
    }

    protected void btnEditKetNoiMang_Click(object sender, EventArgs e) 
    {
        hdnPart.Value = "3";
        hdnIDMau.Value = Session["idMau"].ToString();
        ckKetNoiMang.Text = ltrKetNoiMang.Text;
        btnEditKetNoiMang.Visible = false;
        divKetNoiMang.Attributes.Add("style", "display: block");
    }

    protected void btnOKKetNoiMang_Click(object sender, EventArgs e)
    {
        string part = hdnPart.Value;
        string idMau = hdnIDMau.Value;
        string[] _param = { "noidung", "noidungdecode" };
        string noiDungDecode = Regex.Replace(Server.HtmlDecode(ckKetNoiMang.Text), "<.*?>", string.Empty);
        string[] _value = { ckKetNoiMang.Text, noiDungDecode };
        string _where = "typeid = " + part + " AND idmau = " + idMau;
        db = new class_database();
        db.UpdateRecord("tbl_ttkt_detail", _param, _value, _where);
        divKetNoiMang.Attributes.Add("style", "display: none");
        Response.Redirect(Request.RawUrl, true);
    }

    protected void btnCancelKetNoiMang_Click(object sender, EventArgs e)
    {
        divKetNoiMang.Attributes.Add("style", "display: none");
        btnEditKetNoiMang.Visible = true;
    }

    protected void btnEditMoTaChiTiet_Click(object sender, EventArgs e) 
    {
        hdnPart.Value = "4";
        hdnIDMau.Value = Session["idMau"].ToString();
        ckMoTaChiTiet.Text = ltrMoTaChiTiet.Text;
        divMoTaChiTiet.Attributes.Add("style", "display: block");
        btnEditMoTaChiTiet.Visible = false;
    }

    protected void btnOKMoTaChiTiet_Click(object sender, EventArgs e)
    {
        string part = hdnPart.Value;
        string idMau = hdnIDMau.Value;
        string[] _param = { "noidung", "noidungdecode" };
        string noiDungDecode = Regex.Replace(Server.HtmlDecode(ckMoTaChiTiet.Text), "<.*?>", string.Empty);
        string[] _value = { ckMoTaChiTiet.Text, noiDungDecode };
        string _where = "typeid = " + part + " AND idmau = " + idMau;
        db = new class_database();
        db.UpdateRecord("tbl_ttkt_detail", _param, _value, _where);
        divMoTaChiTiet.Attributes.Add("style", "display: none");
        Response.Redirect(Request.RawUrl, true);
    }

    protected void btnCancelMoTaChiTiet_Click(object sender, EventArgs e)
    {
        divMoTaChiTiet.Attributes.Add("style", "display: none");
        btnEditMoTaChiTiet.Visible = true;
    }

    protected void btnHanhViMalware_Click(object sender, EventArgs e) 
    {
        hdnPart.Value = "5";
        hdnIDMau.Value = Session["idMau"].ToString();
        ckHanhVi.Text = ltrHanhViMalware.Text;
        divHanhVi.Attributes.Add("style", "display: block");
        btnEditHanhViMalware.Visible = false;
    }

    protected void btnOKHanhVi_Click(object sender, EventArgs e)
    {
        string part = hdnPart.Value;
        string idMau = hdnIDMau.Value;
        string[] _param = { "noidung", "noidungdecode" };
        string noiDungDecode = Regex.Replace(Server.HtmlDecode(ckHanhVi.Text), "<.*?>", string.Empty);
        string[] _value = { ckHanhVi.Text, noiDungDecode };
        string _where = "typeid = " + part + " AND idmau = " + idMau;
        db = new class_database();
        db.UpdateRecord("tbl_ttkt_detail", _param, _value, _where);
        divHanhVi.Attributes.Add("style", "display: none");
        Response.Redirect(Request.RawUrl, true);
    }

    protected void btnCancelHanhVi_Click(object sender, EventArgs e)
    {
        divHanhVi.Attributes.Add("style", "display: none");
        btnEditHanhViMalware.Visible = true;
    }


    private void LoadData(int typePart, string idMau)
    {
        string sql = "SELECT noidung FROM tbl_ttkt_detail WHERE idmau = " + idMau + " AND typeid = " + typePart;
        db = new class_database();
        DataTable dt = db.GetDataTable(sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            if (typePart == 1)
            {
                ltrHanhViKyThuat.Text = dt.Rows[0]["noidung"].ToString();
            }
            else if (typePart == 2)
            {
                ltrKeyDacBiet.Text = dt.Rows[0]["noidung"].ToString();
            }
            else if (typePart == 3)
            {
                ltrKetNoiMang.Text = dt.Rows[0]["noidung"].ToString();
            }
            else if (typePart == 4)
            {
                ltrMoTaChiTiet.Text = dt.Rows[0]["noidung"].ToString();
            }
            else if (typePart == 5)
            {
                ltrHanhViMalware.Text = dt.Rows[0]["noidung"].ToString();
            }
        }
    }

    protected void rptItemDataBond(object sender, RepeaterItemEventArgs e)
    {

    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        string part = hdnPart.Value;
        string idMauToEdit = hdnIDMau.Value;

        db = new class_database();
        string[] _param = { "noidung" };
        string[] _value = { CKEditor1.Text };
        string _where = "typeid = " + part + " AND idmau = " + idMauToEdit;
        db.UpdateRecord("tbl_ttkt_detail", _param, _value, _where);
        divPopupEdit.Attributes.Add("style", "display: none");
        Response.Redirect(Request.RawUrl, true);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        divPopupEdit.Attributes.Add("style", "display: none");
    }
    
}